import { PRIORITY_VIEW } from "../../../Core/CoreDefine";
import { GCtrl } from "../../../Core/GCtrl";
import GViewBase from "../../../Core/GView/GViewBase";
import { CMsg, RPointMask, SPECIALWINDOW, VIPBOXTYPE, VIPRECEIVE } from "../../Common/Define";
import { VIEW_ID } from "../../Common/UI";
import { Res } from "../../Common/UIResources";
import GameMgr from "../../Logic/GameMgr";
import HangPage from "../Common/HangPage";
import TopUiItem from "../Common/TopUiItem";
import VipPage from "./VipPage";


const { ccclass, property, menu, executeInEditMode } = cc._decorator;
@ccclass @menu('View/Home/VipCtrl')

export default class VipCtrl extends GViewBase {
    @property({
        type: cc.PageView,
        tooltip: "Vip滚动"
    })
    pageView: cc.PageView = null;

    @property({
        type: cc.Node,
        tooltip: "翻页父节点"
    })
    content: cc.Node = null;

    @property({
        type: cc.Label,
        tooltip: "当前vip等级"
    })
    curvipLv: cc.Label = null;

    @property({
        type: cc.Label,
        tooltip: "下一个vip等级"
    })
    nextLv: cc.Label = null;

    @property({
        type: cc.Label,
        tooltip: "当前观看视频次数"
    })
    curAdNum: cc.Label = null;

    @property({type: cc.Node, tooltip: "充值模块"})  rechargeNode: cc.Node = null;

    @property({type: cc.Node, tooltip: "王权模块"})  vipNode: cc.Node = null;

    /**加载进度条 */
    @property(cc.ProgressBar) progressBar: cc.ProgressBar = null;

    /**当前页面索引 */
    private _index: number = 0;
    /**最大页面索引 */
    private _maxIndex: number = 0;

    /**下一页 */
    protected nextPage() {
        this._maxIndex = this.content.children.length;
        this._index = this.pageView.getCurrentPageIndex();
        this._index++;
        if (this._index >= this._maxIndex) {
            this._index = this._maxIndex;
        }
        this.pageView.setCurrentPageIndex(this._index);
    }

    /**上一页 */
    protected previousPage() {
        this._index = this.pageView.getCurrentPageIndex();
        this._index--;
        if (this._index <= 0) {
            this._index = 0;
        }
        this.pageView.setCurrentPageIndex(this._index);
    }

    onGLoad() {
        GCtrl.ES.on(CMsg.client.home.onVipLvUp, this, this.initUserLvInfo.bind(this), PRIORITY_VIEW)
        this.creatorPage();
        this.initUserLvInfo();
        this.addHang();
    }

    public addHang() {
            /**顶部UI */
            var self = this;
            this.addGChild<TopUiItem>(Res.prefab.item.topUiItem, (comp: TopUiItem) => {
                //comp.node.parent = GameMgr.uiMgr.uiRoot;
                comp.node.parent = self.bgImage.node;
                let winSize = cc.winSize;
                // comp.node.setPosition(cc.v2(-winSize.width / 2, winSize.height / 2));
                comp.setModeType(1, "充值")
                comp.node.setPosition(cc.v2(-winSize.width / 370, winSize.height / 2.16));
            });
    }

    protected changePage() {
        if (GameMgr.lUserData.vipLv >= 1) {
            this._index = GameMgr.lUserData.vipLv - 1;//示例 vip0 升级到 VIP1  page索引为0 展示 vip1的增益
            this.pageView.setCurrentPageIndex(this._index);
        }
    }

    protected setLoadProgress(val: number) {
        if (!this.progressBar) return;
        this.progressBar.progress = Math.min(val, 1);
    }

    protected initUserLvInfo() {
        GameMgr.redMgr.sendValueSettingMsg(RPointMask.RPM_Vip_Daily, false)
        GameMgr.redMgr.sendValueSettingMsg(RPointMask.RPM_Vip_Frist, false)
        var vipLv = GameMgr.lUserData.vipLv;//0~5

        this.curvipLv.string = vipLv + '';
        let nextVip = vipLv + 1;
        if (nextVip > GameMgr.vipData.size) {
            nextVip = vipLv;
            this.nextLv.string = 'vip等级已达最高级';
        } else {
            let next = GameMgr.vipData.getRaw<SVipDataRaw>(nextVip).next;
            let adWatchTime = GameMgr.lUserData.adWatchTime;
            let num = next - adWatchTime;
            // console.log('next - adWatchTime', next - adWatchTime);

            this.nextLv.string = '观看' + (num + 1) + '次广告达到' + 'Vip' + nextVip + '';
        }

        if (vipLv == 0 && GameMgr.lUserData.adWatchTime == 0) {
            if (this.progressBar) this.progressBar.progress = 0;
        } else {
            if (GameMgr.lUserData.adWatchTime > 0) {
                GameMgr.vipData.data.forEach((data: SVipDataRaw, index: number) => {
                    // console.log("index", index, vipLv);

                    if (data.id == vipLv + 1) {//|| data.id == GameMgr.vipData.data.size
                        let val = GameMgr.lUserData.adWatchTime / (data.next + 1);
                        // console.log("setLoadProgress", val, GameMgr.lUserData.adWatchTime, data.next);
                        this.setLoadProgress(val);

                    }
                    if (vipLv >= 1 && index < vipLv) {
                        // console.log("vipLv", index, vipLv);
                        this.changePage();
                        let type = GameMgr.lUserData.getVipReward(index - 1, VIPBOXTYPE.DAYBOX);
                        let firstType = GameMgr.lUserData.getVipReward(index - 1, VIPBOXTYPE.FIRSTBOX);
                        if (type == VIPRECEIVE.No) {
                            GameMgr.lUserData.setVipReward(index - 1, VIPBOXTYPE.DAYBOX, VIPRECEIVE.FIRSTNORECEIVE);
                        }
                        if (firstType == VIPRECEIVE.No || firstType == VIPRECEIVE.DAYRECEIVE) {
                            if (!GameMgr.lUserData.todayIsDayReceiveDate(index - 1)) {
                                GameMgr.lUserData.setVipReward(index - 1, VIPBOXTYPE.FIRSTBOX, VIPRECEIVE.DATNORECEIVE);
                            }
                        }
                    }
                })
            }
        }
    }

    /**看视频加vip */
    protected addVideo() {
        let cost = [SPECIALWINDOW.VIDEO, SPECIALWINDOW.VIDEO, 1];
        GameMgr.jumpToMgr.jumpGoTo(VIEW_ID.currencyFrame, cost, 1);

        // GameMgr.sdkMgr.watchAd(() => {
        //     this.initUserLvInfo();
        // })
    }

    protected textadBtn() {
        GameMgr.lUserData.addAdWatchTime();
        this.initUserLvInfo();
    }

    /**创建页面 */
    protected creatorPage() {
        this.assetImpl.prefab(Res.prefab.vw.home.vipPage, (prefab) => {
            GameMgr.vipData.data.forEach((data: SVipDataRaw, index: number) => {
                let comp = this.assetImpl.addGChild<VipPage>(prefab)
                comp.init(data, index);
                this.pageView.addPage(comp.node);
            })
            // this.changePage();
        })
    }

    close() {
        this.onClose();
    }

    update() {
        //这个千万不能删
        this.pageView._unregisterEvent();
        this.pageView.setCurrentPageIndex(this._index);
    }

    onClickHanlder(event:Event, str:string){
        if (str == "rechargeTog"){
            this.rechargeNode.active = true;
            this.vipNode.active = false;
        }
        else if (str == "vipTog"){
            this.rechargeNode.active = false;
            this.vipNode.active = true;
        }
    }
}